# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  
@software: PyCharm 
@file: class_format.py 
@time: 2019/6/11 13:16
"""
'''
自定义字符串的格式化
'''

_formats = {
    'ymd': '{d.year}-{d.month}-{d.day}',
    'mdy': '{d.month}/{d.day}/{d.year}',
    'dmy': '{d.day}/{d.month}/{d.year}',
}

class Date:
    def __init__(self, year, month, day):
        self.year  = year
        self.month = month
        self.day = day

    def __format__(self, code):
        if code == '':
            code = 'ymd'

        fmt = _formats[code]
        return fmt.format(d=self)


d = Date(2019, 6, 11)
print(format(d))
print(format(d, 'mdy'))
print('The date is {:ymd}'.format(d))
print('The date is {:dmy}'.format(d))
